package com.ruoyi.system.recovery.service.impl;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.zyl.InventoryOrderStatusEnum;
import com.ruoyi.common.enums.zyl.InventoryStatusEnum;
import com.ruoyi.common.enums.zyl.ServiceProblemPlanEnum;
import com.ruoyi.common.enums.zyl.ServiceStatusEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.recovery.domain.RecoveryService;
import com.ruoyi.system.recovery.domain.vo.RecoveryInfoVo;
import com.ruoyi.system.recovery.domain.vo.RecoveryServiceVo;
import com.ruoyi.system.recovery.mapper.RecoveryServiceMapper;
import com.ruoyi.system.recovery.service.IRecoveryInfoService;
import com.ruoyi.system.recovery.service.IRecoveryServiceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 售后Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RecoveryServiceServiceImpl extends ServiceImpl<RecoveryServiceMapper, RecoveryService> implements IRecoveryServiceService {

    private final IRecoveryInfoService  recoveryInfoService;
    /**
     * 查询售后
     * 
     * @param id 售后主键
     * @return 售后
     */
    @Override
    public RecoveryService selectRecoveryServiceById(Long id) {
        return super.getById(id);
    }

    /**
     * 查询售后列表
     * 
     * @param recoveryService 售后
     * @return 售后
     */
    @Override
    public List<RecoveryServiceVo> selectRecoveryServiceList(RecoveryService recoveryService) {
        return baseMapper.selectRecoveryServiceList(recoveryService);
    }

    /**
     * 新增售后
     * 
     * @param recoveryService 售后
     * @return 结果
     */
    @Override
    public boolean insertRecoveryService(RecoveryService recoveryService) {
        return super.save(recoveryService);
    }

    /**
     * 修改售后
     * 
     * @param recoveryService 售后
     * @return 结果
     */
    @Override
    public boolean updateRecoveryService(RecoveryService recoveryService) {
        return super.updateById(recoveryService);
    }

    /**
     * 批量删除售后
     * 
     * @param ids 需要删除的售后主键
     * @return 结果
     */
    @Override
    public boolean deleteRecoveryServiceByIds(Long[] ids) {
        return super.removeByIds(Arrays.asList(ids));
    }

    /**
     * 删除售后信息
     * 
     * @param id 售后主键
     * @return 结果
     */
    @Override
    public boolean deleteRecoveryServiceById(Long id) {
        return super.removeById(id);
    }

    @Override
    public boolean handleService(RecoveryService recoveryService) {
        recoveryService.setStatus(ServiceStatusEnum.PROCESSING); // 处理中
        return recoveryService.updateById();
    }

    @Override
    public boolean cancelService(RecoveryService recoveryService) {
        recoveryService.setStatus(ServiceStatusEnum.CANCEL);
        return recoveryService.updateById();
    }

    @Override
    public AjaxResult endService(RecoveryService recoveryService) {
        RecoveryInfoVo recoveryInfo = recoveryInfoService.selectRecoveryInfoById(recoveryService.getRecoveryInfoId());
        if(recoveryInfo == null){
            return AjaxResult.error("未找到账号信息");
        }
        switch (recoveryService.getServicePlan()) {
            case UNSOLVABLE:
                // 无解》变为问题账号
                recoveryInfo.setInventoryStatus(InventoryStatusEnum.PROBLEM_ACCOUNT);
                recoveryInfo.setRecoveryStatus(InventoryOrderStatusEnum.PROBLEM_ACCOUNT);
                recoveryInfo.updateById();
                break;
            case RESTITUTE:
                recoveryInfo.setInventoryStatus(InventoryStatusEnum.INVALID_ACCOUNT);
                recoveryInfo.setRecoveryStatus(InventoryOrderStatusEnum.INVALID_ACCOUNT);
                recoveryInfo.updateById();
                break;
            case RETURN_ACCOUNT:
                recoveryInfo.setInventoryStatus(InventoryStatusEnum.PENDING_PROCESSING);
                recoveryInfo.setRecoveryStatus(InventoryOrderStatusEnum.CANCEL);
                recoveryInfo.updateById();
                break;
        }

        return recoveryService.updateById() ? AjaxResult.success() : AjaxResult.error();
    }

}
